package cn.jxau.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.jxau.bean.po.User;

/**
 * 管理员权限验证
 * @author licho
 *
 */
public class AdminAccessControlInterceptor implements HandlerInterceptor {
	private static final Logger log=Logger.getLogger(AdminAccessControlInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("user");
		if(null==user ||(!user.getRoleID().equals(2L))){
			if(log.isDebugEnabled()){
				log.debug("我拦截了请求："+request.getServletPath());
			}
			response.sendRedirect(request.getContextPath()+"/jsp/login");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
